package com.cyy.springbootinit.service.impl;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.springbootinit.common.ErrorCode;
import com.cyy.springbootinit.constant.CommonConstant;
import com.cyy.springbootinit.exception.ThrowUtils;
import com.cyy.springbootinit.mapper.SystemNotificationMapper;
import com.cyy.springbootinit.model.dto.systemnotifiaction.SystemNotificationQueryRequest;
import com.cyy.springbootinit.model.entity.SystemNotification;
import com.cyy.springbootinit.model.entity.User;
import com.cyy.springbootinit.model.vo.SystemNotificationVO;
import com.cyy.springbootinit.model.vo.UserVO;
import com.cyy.springbootinit.service.SystemNotificationService;
import com.cyy.springbootinit.service.UserService;
import com.cyy.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统通知表服务实现
 *
 * @author 程序员cyy</a>
 * @from cyy</a>
 */
@Service
@Slf4j
public class SystemNotificationServiceImpl extends ServiceImpl<SystemNotificationMapper, SystemNotification> implements SystemNotificationService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param systemNotification
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validSystemNotification(SystemNotification systemNotification, boolean add) {


        ThrowUtils.throwIf(systemNotification == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String title = systemNotification.getTitle();
        String content = systemNotification.getContent();
        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
        if (StringUtils.isNotBlank(title)) {
            ThrowUtils.throwIf(title.length() > 80, ErrorCode.PARAMS_ERROR, "标题过长");
        }
    }

    /**
     * 获取查询条件
     *
     * @param systemNotificationQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<SystemNotification> getQueryWrapper(SystemNotificationQueryRequest systemNotificationQueryRequest) {
        QueryWrapper<SystemNotification> queryWrapper = new QueryWrapper<>();
        if (systemNotificationQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Long id = systemNotificationQueryRequest.getId();
        String title = systemNotificationQueryRequest.getTitle();
        String content = systemNotificationQueryRequest.getContent();
        Date createTime = systemNotificationQueryRequest.getCreateTime();
        Date updateTime = systemNotificationQueryRequest.getUpdateTime();
        Integer isDelete = systemNotificationQueryRequest.getIsDelete();
        String searchText = systemNotificationQueryRequest.getSearchText();
        String sortOrder = systemNotificationQueryRequest.getSortOrder();
        String sortField = systemNotificationQueryRequest.getSortField();
        if(sortField==null){
            sortField="createTime";
            sortOrder=CommonConstant.SORT_ORDER_DESC;
        }
        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("content", searchText));
        }
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        // JSON 数组查询
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取系统通知表封装
     *
     * @param systemNotification
     * @param request
     * @return
     */
    @Override
    public SystemNotificationVO getSystemNotificationVO(SystemNotification systemNotification, HttpServletRequest request) {
        // 对象转封装类
        SystemNotificationVO systemNotificationVO = SystemNotificationVO.objToVo(systemNotification);

        return systemNotificationVO;
    }

    /**
     * 分页获取系统通知表封装
     *
     * @param systemNotificationPage
     * @param request
     * @return
     */
    @Override
    public Page<SystemNotificationVO> getSystemNotificationVOPage(Page<SystemNotification> systemNotificationPage, HttpServletRequest request) {
        List<SystemNotification> systemNotificationList = systemNotificationPage.getRecords();
        Page<SystemNotificationVO> systemNotificationVOPage = new Page<>(systemNotificationPage.getCurrent(), systemNotificationPage.getSize(), systemNotificationPage.getTotal());
        if (CollUtil.isEmpty(systemNotificationList)) {
            return systemNotificationVOPage;
        }
        // 对象列表 => 封装对象列表
        List<SystemNotificationVO> systemNotificationVOList = systemNotificationList.stream().map(systemNotification -> {
            return SystemNotificationVO.objToVo(systemNotification);
        }).collect(Collectors.toList());
        systemNotificationVOPage.setRecords(systemNotificationVOList);
        return systemNotificationVOPage;
    }

}
